package com.malou.mventas.server.login;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.malou.server.base.Conexion;

import flexjson.JSONSerializer;

public class Usuarios {
	public static SqlMapClient sqlMap = null;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static boolean validarresponsable(String user, String pw){
		boolean retorno = false;
		try {			
	        sqlMap = Conexion.getSqlMap();        
	        
	    	Map m = new HashMap(2);
	    	m.put("user", user);
	    	m.put("pw", pw);	    	
	    	
	    	Integer cant = (Integer) sqlMap.queryForObject("validarresponsable", m);
			if(cant > 0)
				retorno = true;	    	
	        
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return retorno;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getTiendas(){
		String jsonResult = null;
		
		sqlMap = Conexion.getSqlMap();
		try {
			List tiendas_data = sqlMap.queryForList("getTiendas", null);			
			JSONSerializer serializer = new  JSONSerializer();
		    String tiendas_json = serializer.exclude("*.class").deepSerialize(tiendas_data);
		    
		    List reportes_data = sqlMap.queryForList("getReportes", null);			
			serializer = new  JSONSerializer();
		    String reportres_json = serializer.exclude("*.class").deepSerialize(reportes_data);
		    
		    jsonResult = "{\"tiendas\":"+tiendas_json+", \"reportes\":"+reportres_json+"}";
		       
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		
		return jsonResult;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void setActualStock(int tienda_id, Date fechainicial){
		 
		try {			
	        sqlMap = Conexion.getSqlMap();
	        
	        sqlMap.startTransaction();
	    	Map m = new HashMap(2);
	    	m.put("tienda_id", tienda_id);
	    	m.put("fechainicial", fechainicial);
	    	
	    	sqlMap.update("setActualStock", m);	    	
						
	    	sqlMap.commitTransaction();
	    	sqlMap.endTransaction();
	        
		} catch (SQLException e) {			
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String getBalanceTienda(Date fechaactual, Integer tienda_id) {
		String jsonResult = null; 
		try {			
	        sqlMap = Conexion.getSqlMap();
	        
	        sqlMap.startTransaction();
	    	Map m = new HashMap(2);
	    	m.put("tienda_id", tienda_id);
	    	m.put("fecharesumen", fechaactual);
	    	
	    	sqlMap.update("setBalance", m);
	    	
	    	List balance_data = sqlMap.queryForList("getBalanceTienda", m);			
			JSONSerializer serializer = new  JSONSerializer();
			jsonResult = serializer.exclude("*.class").deepSerialize(balance_data);
	    	
	    	sqlMap.commitTransaction();
	    	sqlMap.endTransaction();
	        
		} catch (SQLException e) {			
			e.printStackTrace();
		}
		return jsonResult;
	}
}
